package com.boc.ewr.business.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.business.entity.EwrSpotCheck;
import com.boc.ewr.business.entity.vo.EwrSpotCheckVo;
import com.boc.ewr.business.service.IEwrSpotCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 省行抽检
 * @Author: jeecg-boot
 * @Date:   2023-05-04
 * @Version: V1.0
 */
@Api(tags="省行抽检")
@RestController
@RequestMapping("/business/ewrSpotCheck")
@Slf4j
public class EwrSpotCheckController extends JeecgController<EwrSpotCheck, IEwrSpotCheckService> {
	@Autowired
	private IEwrSpotCheckService ewrSpotCheckService;
	 @Autowired
	 private ISysBaseAPI sysBaseAPI;
	/**
	 * 分页列表查询
	 *
	 * @param ewrSpotCheck
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "省行抽检-分页列表查询")
	@ApiOperation(value="省行抽检-分页列表查询", notes="省行抽检-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<EwrSpotCheckVo>> queryPageList(EwrSpotCheck ewrSpotCheck,
													   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													   HttpServletRequest req) {
		QueryWrapper<EwrSpotCheck> queryWrapper = QueryGenerator.initQueryWrapper(ewrSpotCheck, req.getParameterMap());
		Set<String> orgCodeSetByLoginUser = sysBaseAPI.getOrgCodeSetByLoginUser(this.getLoginUser());
		if (orgCodeSetByLoginUser!=null) {
			queryWrapper.in("sc_branch_no", orgCodeSetByLoginUser);
		}
		Page<EwrSpotCheck> page = new Page<EwrSpotCheck>(pageNo, pageSize);
		IPage<EwrSpotCheck> pageList = ewrSpotCheckService.page(page, queryWrapper);
		IPage<EwrSpotCheckVo> convertIPage = this.convertInfoPage(pageList);
		return Result.OK(convertIPage);
	}

	/**
	 *   添加
	 *
	 * @param ewrSpotCheck
	 * @return
	 */
	@AutoLog(value = "省行抽检-添加")
	@ApiOperation(value="省行抽检-添加", notes="省行抽检-添加")
	//@RequiresPermissions("org.jeecg.modules:ewr_spot_check:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody EwrSpotCheck ewrSpotCheck) {
		//获取反馈人和反馈时间
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		ewrSpotCheck.setCreateName(user.getRealname());
		ewrSpotCheck.setCreateTime(new Date());
		ewrSpotCheckService.save(ewrSpotCheck);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param ewrSpotCheck
	 * @return
	 */
	@AutoLog(value = "省行抽检-编辑")
	@ApiOperation(value="省行抽检-编辑", notes="省行抽检-编辑")
	//@RequiresPermissions("org.jeecg.modules:ewr_spot_check:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody EwrSpotCheck ewrSpotCheck) {
		ewrSpotCheckService.updateById(ewrSpotCheck);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "省行抽检-通过id删除")
	@ApiOperation(value="省行抽检-通过id删除", notes="省行抽检-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:ewr_spot_check:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		ewrSpotCheckService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "省行抽检-批量删除")
	@ApiOperation(value="省行抽检-批量删除", notes="省行抽检-批量删除")
	//@RequiresPermissions("org.jeecg.modules:ewr_spot_check:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.ewrSpotCheckService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "省行抽检-通过id查询")
	@ApiOperation(value="省行抽检-通过id查询", notes="省行抽检-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<EwrSpotCheck> queryById(@RequestParam(name="id",required=true) String id) {
		EwrSpotCheck ewrSpotCheck = ewrSpotCheckService.getById(id);
		if(ewrSpotCheck==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ewrSpotCheck);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ewrSpotCheck
    */
    //@RequiresPermissions("org.jeecg.modules:ewr_spot_check:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, EwrSpotCheck ewrSpotCheck) {
        return super.exportXls(request, ewrSpotCheck, EwrSpotCheck.class, "省行抽检");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("ewr_spot_check:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, EwrSpotCheck.class);
    }
	 private LoginUser getLoginUser() {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 return sysUser;
	 }

	/**
	 * 将 Page转出成Vo的Page
	 */
	private IPage<EwrSpotCheckVo> convertInfoPage(IPage<EwrSpotCheck> page) {
		List<EwrSpotCheckVo> voList = page.getRecords().stream()
				.map(this::convertESC)
				.collect(Collectors.toList());

		IPage<EwrSpotCheckVo> voPage = new Page<>();
		voPage.setCurrent(page.getCurrent());
		voPage.setSize(page.getSize());
		voPage.setTotal(page.getTotal());
		voPage.setRecords(voList);

		return voPage;
	}

	private EwrSpotCheckVo convertESC(EwrSpotCheck entity) {
		EwrSpotCheckVo vo = new EwrSpotCheckVo();
		BeanUtils.copyProperties(entity, vo);
		return vo;
	}
}
